<template>
  <div id="app" :class="{ 'is-MAPP': $store.state.isMAPP }">
    <MTabbar />
    <router-view
      class="page"
      :class="[
        { 'has-tabbar': $route.meta.tabbar && !$store.state.isMAPP },
        $route.name
      ]"
    />
    <transition name="popup">
      <div
        class="login-pop"
        :class="{ 'is-visible': user.showLoginPop || show }"
        v-if="user.showLoginPop || show"
      >
        <div class="header">
          <van-icon
            class="close-btn"
            name="cross"
            @click="$store.dispatch('user/closeLoginPop')"
          />
        </div>
        <LoginPop :onLogin="onLogin" />
      </div>
    </transition>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import MTabbar from 'biz/tabbar'
import LoginPop from './views/account/login'
import api from '@/api'

export default {
  name: 'App',
  components: {
    MTabbar,
    LoginPop
  },
  data() {
    return {
      show: false
    }
  },
  computed: {
    ...mapState({
      user: state => state.user,
      userInfo: state => state.user.userInfo,
      isValidLogin: state => state.user.isValidLogin
    })
  },
  methods: {
    onLogin() {
      this.show = false
      this.init()
    },
    init() {
      api.checkLogin().then(res => {
        if (res.data.status === 1 && this.userInfo) {
          this.$store.dispatch('ch/initSocket')
          this.$store.dispatch('ch/getNewFriend')
          this.$store.dispatch('ch/getFriendList')
        } else {
          this.show = true
        }
      })
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style lang="less" scoped>
@import './assets/styles/vars.less';
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.zero-box {
  width: 100vw;
  height: 100vh;
  text-align: center;
  color: #999;
  img {
    width: 200px;
    display: block;
    margin: 0 auto;
  }
}

.login-pop {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 998;
  background: #fff;
}

.has-tabbar {
  padding-bottom: 50px;
}

.popup-enter-active,
.popup-leave-active {
  transition: all 0.5s ease;
}

.popup-enter,
.popup-leave-to {
  opacity: 0;
  top: 100vh;
}

.view-login {
  overflow: hidden;
}

.header {
  text-align: right;
  padding-right: 15px;
  margin-bottom: 30px;
}

.close-btn {
  font-size: 24px;
  margin-left: 15px;
  margin-top: 12px;
  color: #c5c4c4;
}
</style>
